package com.wz.dhs.model;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

import java.util.HashMap;
import java.util.Map;

/**
 * 通用响应结构
 * @author caiwenhao
 * @date 2020年3月11日 上午10:48:11
 * @see [相关类/方法]
 */
@JsonIgnoreProperties(ignoreUnknown = true)
public class GeneralResult {
    // 结果状态, true 成功 false 失败
    private Boolean resultStatus;

    // 如果错误则返回 错误代码
    private String errorCode;

    // 错误详情描述
    private String errorMessage;

    // 结果为true时的 业务数据，可为空
    private Object resultData;

    private Map<String, Object> resMap;

    public GeneralResult()
    {
    }

    public GeneralResult(ErrorCode error)
    {
        this(Boolean.FALSE, error.getCode(), error.getMsg());
    }

    public GeneralResult(ErrorCode error, Object obj)
    {
        this(Boolean.FALSE, error.getCode(), error.getMsg());
        this.resultData = obj;
    }

    public GeneralResult(String errorCode, String errorMessage)
    {
        super();
        this.resultStatus = Boolean.FALSE;
        this.errorCode = errorCode;
        this.errorMessage = errorMessage;
    }

    public GeneralResult(Boolean resultStatus, String errorCode, String errorMessage)
    {
        super();
        this.resultStatus = resultStatus;
        this.errorCode = errorCode;
        this.errorMessage = errorMessage;
    }

    public GeneralResult ok() {
        this.resultStatus = Boolean.TRUE;
        return this;
    }

    public GeneralResult response(Object resultData) {
        this.resultData = resultData;
        this.resultStatus = Boolean.TRUE;
        return this;
    }

    public GeneralResult responseMap() {
        this.resMap = new HashMap<String, Object>();
        this.resultData = this.resMap;
        this.resultStatus = Boolean.TRUE;
        return this;
    }

    public GeneralResult addParam(String key, Object value) {
        this.resMap.put(key, value);
        return this;
    }

    public GeneralResult addParams(Map<String, Object> params) {
        this.resMap.putAll(params);
        return this;
    }

    public static GeneralResult operateSuccess(){
        GeneralResult generalResult = new GeneralResult();
        return generalResult.ok();
    }

    public static GeneralResult operateSuccess(Object resultData){
        GeneralResult generalResult = new GeneralResult();
        return generalResult.response(resultData);
    }

    public Boolean getResultStatus() {
        return resultStatus;
    }

    public String getErrorCode() {
        return errorCode;
    }

    public String getErrorMessage() {
        return errorMessage;
    }

    public Object getResultData() {
        return resultData;
    }

    @Override
    public String toString() {
        return "CommonResult [resultStatus=" + resultStatus + ", errorCode=" + errorCode + ", errorMessage="
            + errorMessage + ", resultData=" + resultData + ", resMap=" + resMap + "]";
    }

}
